package com.lechuang.songbo.utils;

import android.content.Context;
import android.content.Intent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.lechuang.songbo.view.activity.own.InfoActivity;
import com.lechuang.songbo.view.activity.own.UserCenterActivity;
import com.lechuang.songbo.view.activity.MyThesunActivity;
import com.lechuang.songbo.view.activity.SigneActivity;
import com.lechuang.songbo.view.activity.own.BoundAlipayActivity;
import com.lechuang.songbo.view.activity.ui.LoginActivity;
import com.lechuang.songbo.view.defineView.ProgressWebView;

/**
 * Created by zhf on 2017/8/19.
 */

public class WebViewUtils {

    public static void loadUrl(ProgressWebView mWeb, final Context context, final String url) {

        //js调用
        mWeb.getSettings().setJavaScriptEnabled(true);
        //是否储存
        mWeb.getSettings().setDomStorageEnabled(false);
        //缓存大小
        //mWeb.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
        //缓存路径
        //String appCachePath = context.getCacheDir().getAbsolutePath();
        //mWeb.getSettings().setAppCachePath(appCachePath);
        //是否禁止访问文件数据
        mWeb.getSettings().setAllowFileAccess(true);
        mWeb.getSettings().setAppCacheEnabled(false);
        mWeb.getSettings().setUseWideViewPort(true);
        mWeb.getSettings().setLoadWithOverviewMode(true);
        //是否支持缩放
        mWeb.getSettings().setSupportZoom(true);
        mWeb.getSettings().setBuiltInZoomControls(true);
        mWeb.getSettings().setDisplayZoomControls(false);
        mWeb.loadUrl(url);
        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        mWeb.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.equals("appfun:sign:reward")) {
                    //跳到签到奖励
                    context.startActivity(new Intent(context, SigneActivity.class));
                } else if (url.equals("appfun:bask:reward")) {
                    //跳到晒单界面
                    context.startActivity(new Intent(context, MyThesunActivity.class));
                } else if (url.equals("appfun:upload:avatar")) {
                    //上传头像
                    context.startActivity(new Intent(context, UserCenterActivity.class));
                } else if (url.equals("appfun:complete:nick")) {
                    //完善昵称
                    context.startActivity(new Intent(context, UserCenterActivity.class));
                } else if (url.equals("appfun:bind:ALI")) {
                    //绑定支付宝
                    context.startActivity(new Intent(context, BoundAlipayActivity.class));
                } else if (url.equals("appfun:bind:taobao")) {
                    //绑定淘宝账号
                    context.startActivity(new Intent(context, LoginActivity.class));
                }else if (url.equals("app:jump:scoreIntro")) {
                    //任务中心积分说明
                    context.startActivity(new Intent(context, InfoActivity.class));
                }/*else if(url.startsWith("appfun:zoomImage:")){
                    Intent intent = new Intent(context, SunBigPicActivity.class);
                    intent.putExtra("live",1);
                    intent.putExtra("bigImg",url.substring(17));
                    context.startActivity(intent);
                }*/else if (url.startsWith("http")) {
                    view.loadUrl(url);
                }
                return true;
            }
        });
    }

}
